<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
    <title></title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport"
        content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
    <link rel="stylesheet" href="/layui/css/layui.css" media="all">
    <link rel="stylesheet" href="/style/admin.css" media="all">
</head>

<body>

    <div class="layui-fluid">
        <div class="layui-row layui-col-space15">
            <div class="layui-col-md12">
                <div class="layui-card">
                    <div class="layui-card-header">备件出入库记录</div>
                    <div class="layui-card-body">
                        <div style="margin-bottom: 10px;">
                            <div class="layui-input-item" style="float:left;padding-right:10px;">
                                <form class="layui-form">
                                    <div class="layui-inline">
                                        <input id="search-name" type="text" placeholder="请输入备件名称"
                                            class="layui-input layui-input-search">
                                    </div>
                                    <div class="layui-inline">
                                        <input id="search-brand" type="text" placeholder="请输入品牌"
                                            class="layui-input layui-input-search">
                                    </div>
                                    <div class="layui-inline">
                                        <input id="search-model" type="text" placeholder="请输入型号/规格"
                                            class="layui-input layui-input-search">
                                    </div>
                                    <div class="layui-inline">
                                        <select id="search-order-type" lay-search>
                                            <option value="">请选择单据类型</option>
                                            <option value="1">采购入库</option>
                                            <option value="2">领用出库</option>
                                            <option value="3">归还入库</option>
                                        </select>
                                    </div>
                                    <div class="layui-inline">
                                        <input id="date-range" type="text" placeholder="选择日期范围" class="layui-input">
                                    </div>
                                    <div class="layui-inline">
                                        <a id="btn-search" class="layui-btn">查找</a>
                                    </div>
                                </form>
                            </div>
                            <div style="float:right;">
                                <a id="btn-batch-delete" class="layui-btn layui-btn-danger">删除</a>
                                <a id="btn-refresh" class="layui-btn layui-btn-primary">刷新</a>
                            </div>
                            <div style="clear:both;"></div>
                        </div>
                        <table class="layui-hide" id="sparepart-inout-stock-table"
                            lay-filter="sparepart-inout-stock-table"></table>
                        <script type="text/html" id="toolbar-operation">
                        <a class="layui-btn layui-btn-xs" lay-event="item-detail">记录详情</a>
                        <a class="layui-btn layui-btn-xs" lay-event="order-detail">单据详情</a>
                        <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
                    </script>
                    </div>
                </div>
            </div>
        </div>
    </div>
    <script type="text/html" id="sparepart-inout-stock-form">
    <div class="layui-card">
        <div class="layui-card-header">备件出入库记录详情</div>
        <div class="layui-card-body">
            <form class="layui-form" action="#" lay-filter="sparepart-inout-stock-form">
                <input type="hidden" name="id">
                <div class="layui-form-item">
                    <label class="layui-form-label">*备件名称</label>
                    <div class="layui-input-block">
                        <input type="text" name="name" class="layui-input" lay-verify="required">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">品牌</label>
                    <div class="layui-input-block">
                        <input type="text" name="brand" class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">型号/规格</label>
                    <div class="layui-input-block">
                        <input type="text" name="model" class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">*出入库数量</label>
                    <div class="layui-input-block">
                        <input type="number" name="quantity" class="layui-input" lay-verify="required">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">单位</label>
                    <div class="layui-input-block">
                        <input type="text" name="unit" class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">出厂编号</label>
                    <div class="layui-input-block">
                        <input type="text" name="productNo" class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">生产日期</label>
                    <div class="layui-input-block">
                        <input type="text" name="productDate" class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">*单据号</label>
                    <div class="layui-input-block">
                        <input type="text" name="orderNo" class="layui-input" lay-verify="required">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">*单据类型</label>
                    <div class="layui-input-block">
                        <input type="text" name="orderTypeText" class="layui-input" lay-verify="required">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">*出入库时间</label>
                    <div class="layui-input-block">
                        <input type="text" name="createTime" class="layui-input" lay-verify="required">
                    </div>
                </div>
            </form>
        </div>
    </div>
</script>
    <script src="/layui/layui.js"></script>
    <script>
        layui.config({
            base: '/' //静态资源所在路径
        }).extend({
            index: 'lib/index' //主入口模块
        }).use(['index', 'table', 'form', 'laydate'], function () {
            var $ = layui.$;
            var table = layui.table;
            var form = layui.form;
            var admin = layui.admin;
            var laydate = layui.laydate;
            var popupIndex;

            laydate.render({
                elem: '#date-range',
                range: true
            });

            table.render({
                elem: '#sparepart-inout-stock-table',
                url: 'sparepart-inout-stocks',
                title: '',
                skin: 'line',
                cols: [[
                    { type: 'checkbox' },
                    {
                        title: '序号', width: 60, templet: function (d) {
                            return d.LAY_TABLE_INDEX + 1;
                        }
                    },
                    { field: 'name', title: '备件名称' },
                    { field: 'brand', title: '品牌' },
                    { field: 'model', title: '型号/规格' },
                    { field: 'displayQuantity', title: '出入库数量', width: 100 },
                    { field: 'unit', title: '单位', width: 100 },
                    { field: 'createTime', title: '出入库时间' },
                    { field: 'orderTypeText', title: '单据类型' },
                    { field: 'orderNo', title: '单据号' },
                    { fixed: 'right', title: '操作', width: 250, toolbar: '#toolbar-operation' }
                ]],
                page: true
            });

            function showSparepartInoutStockForm() {
                popupIndex = admin.popupRight({
                    area: '360px',
                    success: function () {
                        var formHtml = $('#sparepart-inout-stock-form').html();
                        $('#' + this.id).html(formHtml);
                        form.render();
                        laydate.render({
                            elem: '#create-time'
                        });
                    }
                });
                return popupIndex;
            }

            function closePopupForm() {
                if (popupIndex) {
                    layer.close(popupIndex);
                }
            }

            function search() {
                table.reload('sparepart-inout-stock-table', {
                    where: {
                        name: $('#search-name').val(),
                        brand: $('#search-brand').val(),
                        model: $('#search-model').val(),
                        orderType: $('#search-order-type').val(),
                        dateRange: $('#date-range').val(),
                    },
                    page: {
                        curr: 1
                    }
                });
            }

            $(document).on('keypress', function (e) {
                if (e.which == 13) {
                    search();
                }
            });

            $("#btn-search").on('click', function () {
                search();
            });

            $("#btn-add").on('click', function () {
                popupIndex = showSparepartInoutStockForm();
            });

            $("#btn-refresh").on('click', function () {
                table.reload('sparepart-inout-stock-table');
                layer.msg('表格已刷新');
            });

            $("#btn-batch-delete").on('click', function () {
                layer.confirm('确定要删除吗?', { icon: 3, title: '提示' }, function (index) {
                    var ids = [];
                    var data = table.checkStatus('sparepart-inout-stock-table').data;
                    for (let i in data) {
                        ids.push(data[i].id);
                    }
                    $.ajax({
                        url: 'sparepart-inout-stocks/' + ids.toString(),
                        type: 'DELETE',
                        success: function (result) {
                            if (result.code == 200) {
                                layer.msg('删除成功');
                                table.reload('sparepart-inout-stock-table');
                            } else {
                                layer.msg('删除失败');
                            }
                        }
                    });
                });
            });

            table.on('tool(sparepart-inout-stock-table)', function (obj) {
                var data = obj.data;
                if (obj.event === 'del') {
                    layer.confirm('真的删除该记录吗？', function (index) {
                        $.ajax({
                            url: 'sparepart-inout-stocks/' + data.id,
                            type: 'DELETE',
                            success: function (result) {
                                if (result.code == 200) {
                                    layer.msg('删除成功');
                                    table.reload('sparepart-inout-stock-table');
                                } else {
                                    layer.msg('删除失败');
                                }
                            }
                        });
                        layer.close(index);
                    });
                } else if (obj.event === 'item-detail') {
                    showSparepartInoutStockForm();
                    form.val('sparepart-inout-stock-form', data);
                } else if (obj.event === 'order-detail') {
                    let url = '';
                    if (data.orderType == 1) {
                        url = 'sparepart-purchase-instock-order-detail.html?no=' + data.orderNo;
                    }
                    if (data.orderType == 2) {
                        url = 'sparepart-use-order-detail.html?no=' + data.orderNo;
                    }
                    if (data.orderType == 3) {
                        url = 'sparepart-return-order-detail.html?no=' + data.orderNo;
                    }
                    layer.open({
                        title: '单据详情',
                        type: 2,
                        content: url,
                        area: ['1400px', '800px'],
                        maxmin: true
                    });
                }
            });
        });
    </script>
</body>

</html>